/*
 * ResultDialog.cs - Wordtrainer
 * 
 * Copyright (C) 1998-2008 Markus Kilås
 *               
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 *
 * Authors:
 *   Markus Kilås <markus@markuspage.com>
 * 
 */

using System;
using System.Collections;
using Gtk;
using Gdk;

namespace WtSharp
{
	
	
	public partial class ResultDialog : Gtk.Dialog
	{
		private TreeStore store;
		private ArrayList m_Words;
		private PractiseInfo m_PractiseInfo;
		private bool m_bRepeat = false;
		private Pixbuf pixbufMarkS;
		private Pixbuf pixbufMarkV;
		
		public ResultDialog(ArrayList words, PractiseInfo info)
		{
			m_Words = words;
			m_PractiseInfo = info;
			this.Build();
			
			NumWords.Text = ""+m_PractiseInfo.WordCount;
			NumRights.Text = ""+m_PractiseInfo.Right;
			NumWrong.Text = ""+m_PractiseInfo.Wrong;
			NumTry2.Text = ""+m_PractiseInfo.Right2nd;

			// Load marks
			pixbufMarkS = Gdk.Pixbuf.LoadFromResource("mark-s.png").ScaleSimple(16,16,Gdk.InterpType.Bilinear);
			pixbufMarkV = Gdk.Pixbuf.LoadFromResource("mark-v.png").ScaleSimple(16,16,Gdk.InterpType.Bilinear);
			
			if(m_PractiseInfo.Wrong < 1)
			{
				buttonRepeat.Sensitive = false;
				buttonSave.Sensitive = false;
				//buttonOk.GrabFocus();
			}
			else
			{
				//buttonRepeat.GrabDefault();
				//buttonRepeat.GrabFocus();
			}
			
			store = new TreeStore (typeof (WtWord), typeof (Gdk.Pixbuf), typeof(string), typeof(string));

			treeview1.Model = store;
			treeview1.HeadersVisible = true;
			treeview1.HeadersClickable = true;
			
			treeview1.AppendColumn("Type", new CellRendererPixbuf(), "pixbuf", 1);
			treeview1.AppendColumn("Foreign word", new CellRendererText(), "text", 2);
			treeview1.AppendColumn("Translation", new CellRendererText(), "text", 3);
			treeview1.ColumnsAutosize();
			PopulateList();
		}
		
		private void PopulateList()
		{
			store.Clear();
			
			Gdk.Pixbuf pixbuff = null;
			foreach(WtWord word in m_Words)
		    {
				if(word.Type == 1)
					pixbuff = pixbufMarkS;
				else if(word.Type == 2)
					pixbuff = pixbufMarkV;
				else
					pixbuff = null;
				store.AppendValues (word, pixbuff, word.WordF, word.WordT);
		    }
			treeview1.Model = store;			
		}
		
		public bool Repeat
		{
			get
			{
				return m_bRepeat;
			}
		}

		protected virtual void OnButtonRepeatClicked (object sender, System.EventArgs e)
		{
			m_bRepeat = true;
			Destroy();
		}

		protected virtual void OnButtonSaveClicked (object sender, System.EventArgs e)
		{
			Gtk.FileChooserDialog fc = new Gtk.FileChooserDialog("Save As",
		                                         this,
		                                         FileChooserAction.Save,
                                                "Cancel",ResponseType.Cancel,
                                                "Save",ResponseType.Accept);
			if(fc.Run() != (int)ResponseType.Accept)
			{
				fc.Destroy();
				return;
			}
			
			WtDocument document = new WtDocument(fc.Filename);
			document.Words = m_Words;
			IFileHandler fileHandler =  FileHandlerFactory.Instance.GetFileHandler(fc.Filename, fc.Filter);
			fileHandler.Save(document, fc.Filename);
			fc.Destroy();
		}

		protected virtual void OnButtonOkClicked (object sender, System.EventArgs e)
		{
			Destroy();
		}
		
	}
}
